home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
FishMarket 1.0
/
FishMarket v1.0.iso
/
fishies
/
301-325
/
disk_319
/
cnewssrc
/
cnews.src.lzh
/
libamiga
/
gethostname.c
< prev
next >
Wrap
C/C++ Source or Header
|
1980-01-01
|
2KB
|
114 lines
/*
* v7 gethostname simulation
* taken from pathalias and cleaned up. Thanks, peter.
*/
#include <stdio.h>
#ifdef AMIGA
# ifndef index
# define index strchr
# define rindex strrchr
# endif
#endif
/* imports from libc */
extern char *index(), *strncpy();
extern FILE *fopen(), *popen();
/* forwards */
void readhostfile();
#define MAXHOST 256
#ifndef min
# define min(a,b) ((a) < (b)? (a): (b))
#endif
static char defhost[] = "ckctpa";
#ifdef AMIGA
static char buff[BUFSIZ];
#else
static char *namefiles[] = {
"/etc/whoami",
"/etc/systemid",
NULL,
};
#endif /* AMIGA */
int
gethostname(hostname, size)
register char *hostname;
int size;
{
#ifdef AMIGA
*hostname = '\0';
/* try the NEWSCTL directory for a file called "whoami" */
strcpy(buff, ctlfile("whoami"));
readhostfile(hostname, size, buff);
if (*hostname != '\0')
return( 0 );
#else
register FILE *whoami;
register char **namep;
*hostname = '\0';
/* try files in namefiles */
for (namep = namefiles; *namep != NULL; namep++) {
readhostfile(hostname, size, *namep);
if (*hostname != '\0')
return 0;
}
/* try /usr/include/whoami.h */
if ((whoami = fopen("/usr/include/whoami.h", "r")) != NULL) {
while (!feof(whoami)) {
char sysname[MAXHOST];
if (fgets(sysname, MAXHOST, whoami) == NULL)
break;
if (sscanf(sysname, "#define sysname \"%[^\"]\"",
hostname) > 0)
break;
}
(void) fclose(whoami);
if (*hostname != '\0')
return 0;
}
/* ask uucp */
if ((whoami = popen("PATH=/bin:/usr/bin:/usr/ucb uuname -l", "r")) != NULL) {
register char *ptr;
(void) fgets(hostname, size, whoami);
(void) pclose(whoami);
if ((ptr = index(hostname, '\n')) != NULL)
*ptr = '\0';
}
if (*hostname != '\0')
return 0;
#endif /* AMIGA */
/* aw hell, i give up! is this a real unix? */
(void) strncpy(hostname, defhost, min(sizeof defhost, size));
return 0;
}
static void
readhostfile(hostname, size, file) /* read host name from file */
char *hostname, *file;
int size;
{
register FILE *whoami;
if ((whoami = fopen(file, "r")) != NULL) {
register char *ptr;
(void) fgets(hostname, size, whoami);
(void) fclose(whoami);
if ((ptr = index(hostname, '\n')) != NULL)
*ptr = '\0';
}
}